package com.homestay.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homestay.beans.UserBean;
import com.homestay.service.IUserService;

/**
 * 处理用户相关的信息
 * @author dyx
 *
 */
@Controller
public class UserAction {

	@Autowired
	private IUserService userService;
	
	/**
	 * 处理用户登录
	 */
	@RequestMapping("/userLogin.action")
	public void userLogin(@RequestParam String username,@RequestParam String password,HttpServletResponse response){		
		String result = userService.userLogin(username, password);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
	/**
	 * 处理用户注册
	 */
	@RequestMapping("/userRegister.action")
	@ResponseBody
	public Map<String, Object> userRegister(@RequestBody UserBean user){
		//调用userService的方法
		String registerResult = userService.userRegister(user);
		//返回的注册结果存放在map中，以json格式返回给前台
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result",registerResult);
		return map;
	}
}
